package com.example.mobile1920605116cxb0604;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button btnMainStartS;
    private Button btnMainStopS;
    MusicService musicService;
    ServiceConnection serviceConnection;
    private Button btnMainPause;
    private Object IBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainStartS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MainActivity.this, MusicService.class);
                serviceConnection = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        musicService = ((MusicService.SerivceBinder) service).getMusicService();
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        musicService = null;
                    }
                };
//                startService(intent);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });

        btnMainStopS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.playMusic();
            }
        });
        btnMainPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.stopMusic();
            }
        });
    }

    private void initView() {
        btnMainStartS = (Button) findViewById(R.id.btnMainStartS);
        btnMainStopS = (Button) findViewById(R.id.btnMainStopS);
        btnMainPause = (Button) findViewById(R.id.btnMainPause);
    }
}
